var parameter = getRequest();
var clientName = parameter['clientname'];//参数名
var millName=parameter['millName'];

$(function () {

    //获取记录集
    getData();

    /*表格数据求和*/

    //成品入库
    $('#waterdetai').datagrid({
        showFooter:true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps = 0;
            var ms = 0;

            for (let i = 0; i < data.length; i++) {
                qty += data[i].b_qty ? parseFloat(data[i].b_qty) : 0;
                ps += data[i].b_ps ? parseFloat(data[i].b_ps) : 0;
                ms += data[i].b_meter ? parseFloat(data[i].b_meter) : 0;

            }
            $(this).datagrid('reloadFooter', [{
                b_date: '合计',
                b_qty: qty.toFixed(1),
                b_ps: ps.toFixed(0),
                b_meter: ms.toFixed(0),
            }])
        }
    })

    //成品出库
    $('#waterdetai01').datagrid({
        showFooter:true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps = 0;
            var ms = 0;

            for (let i = 0; i < data.length; i++) {
                qty += data[i].b_qty ? parseFloat(data[i].b_qty) : 0;
                ps += data[i].b_ps ? parseFloat(data[i].b_ps) : 0;
                ms += data[i].b_meter ? parseFloat(data[i].b_meter) : 0;

            }
            $(this).datagrid('reloadFooter', [{
                b_date: '合计',
                b_qty: qty.toFixed(1),
                b_ps: ps.toFixed(0),
                b_meter: ms.toFixed(0),
            }])
        }
    })

    //成品库存
    $('#waterdetai02').datagrid({
        showFooter:true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps = 0;
            var ms = 0;

            for (let i = 0; i < data.length; i++) {
                qty += data[i].b_qty ? parseFloat(data[i].b_qty) : 0;
                ps += data[i].b_ps ? parseFloat(data[i].b_ps) : 0;
                ms += data[i].b_meter ? parseFloat(data[i].b_meter) : 0;

            }
            $(this).datagrid('reloadFooter', [{
                b_pinming: '合计',
                b_qty: qty.toFixed(1),
                b_ps: ps.toFixed(0),
                b_meter: ms.toFixed(0),
            }])
        }
    })

    showColor();

    /**********************************************************/
})


function getData() {
console.log('https://www.xdfznh.club/wxpadbe/execute/query/'
    + millName
    + "/call PurchaseAndSaleDetail('"
    + getUrlParam("Sdate").substr(0,10)
    + "','"
    + getUrlParam("Edate").substr(0,10)
    + "',"
    + "'" + clientName + "'"
    + ",'白坯')");

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/execute/query/'
            + millName
            + "/call PurchaseAndSaleDetail('"
            + getUrlParam("Sdate").substr(0,10)
            + "','"
            + getUrlParam("Edate").substr(0,10)
            + "',"
            + "'" + clientName + "'"
            + ",'白坯')",

        type: 'get',
        dataType: 'json',
        success: function (data) {
            var data1;
            var data2;
            var data3;

            console.log(11111)

            data1=data.filter(item=>item.b_detailtype==='白坯入库');
            data2=data.filter(item=>item.b_detailtype==='白坯领料');
            data3=data.filter(item=>item.b_detailtype==='白坯库存');


            $('#waterdetai').datagrid('loadData', data1);
            $('#waterdetai01').datagrid('loadData', data2);
            $('#waterdetai02').datagrid('loadData', data3);

            showColor();
        }
    })
}




//获取浏览器地址参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

//获取浏览器地址参数（参数为中文）
function getRequest() {
    var url = window.location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            //就是这句的问题
            theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
            //之前用了unescape()
            //才会出现乱码
        }
    }
    return theRequest;
}


//根据条件改变背景 色
function showColor() {
    $('#waterdetai').datagrid({
        rowStyler: function (index, row) {
            if (row.b_order > 0) {
                return 'color:#FF0000 ';   //#E0ECFF
            }
        }
    });
}







